<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var maxEnvelopes = function (envelopes) {
      const nums = envelopes.sort((e1, e2) => e1[0] !== e2[0] ? e1[0] - e2[0] : e2[1] - e1[1]).map(e => e[1]);
      const top = [];
      let piles = 0;
      for (let i = 0; i < nums.length; i++) {
        let poker = nums[i];
        let left = 0, right = piles;
        while (left < right) {
          let mid = left + ((right - left) >> 1);
          if (top[mid] >= poker) {
            right = mid;
          } else {
            left = mid + 1;
          }
        }
        if (left === piles) {
          piles++;
        }
        top[left] = poker;
      }
      return piles;
    };
    console.log(maxEnvelopes([[5, 4], [6, 4], [6, 7], [2, 3]]))
  </script>
</body>

</html>